<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : historibayar  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrhistoribayar extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
//  $this->load->view('test.php');
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformhistoribayar('0', $xAwal);
    }

    function createformhistoribayar($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        $xAddJs = '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/baseurl.js"></script>' .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxhistoribayar.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormhistoribayar($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormhistoribayar($xidx) {
        $this->load->helper('form');
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>Histori Bayar</h3><div class="garis"></div>' . form_open_multipart('ctrhistoribayar/inserttable', array('id' => 'form', 'name' => 'form'));
        $this->load->helper('common');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
//        $xBufResult .= setForm('edidcustomer', 'idcustomer', form_input(getArrayObj('edidcustomer', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpaket', 'idpaket', form_input(getArrayObj('edidpaket', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edjenisbayar', 'jenisbayar', form_input(getArrayObj('edjenisbayar', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edtotal', 'total', form_input(getArrayObj('edtotal', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('eddate_bayar', 'date_bayar', form_input(getArrayObj('eddate_bayar', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
        //$xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'simpan', 'onclick="dosimpanhistoribayar();"') . form_button('btNew', 'new', 'onclick="doClearhistoribayar();"') . '<div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '') . '</div><div class="spacer"></div>';
        $xBufResult .= '<div class="garis"></div><div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '') . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getlisthistoribayar($xAwal, $xSearch) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('idx', '', 'width=10%') .
                tbaddcell('idcustomer', '', 'width=10%') .
                tbaddcell('idpaket', '', 'width=10%') .
                tbaddcell('jenisbayar', '', 'width=10%') .
                tbaddcell('total', '', 'width=10%') .
                tbaddcell('date_bayar', '', 'width=10%') .
                tbaddcell('idpegawai', '', 'width=10%') .
                tbaddcell('Edit/Hapus', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelhistoribayar');
        $xQuery = $this->modelhistoribayar->getListhistoribayar($xAwal, $xLimit, $xSearch);
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doedithistoribayar(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapushistoribayar(\'' . $row->idx . '\',\'' . substr($row->idcustomer, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($row->idx) .
                    tbaddcell($row->idcustomer) .
                    tbaddcell($row->idpaket) .
                    tbaddcell($row->jenisbayar) .
                    tbaddcell($row->total) .
                    tbaddcell($row->date_bayar) .
                    tbaddcell($row->idpegawai) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchhistoribayar(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchhistoribayar(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchhistoribayar(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editrechistoribayar() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelhistoribayar');
        $row = $this->modelhistoribayar->getDetailhistoribayar($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['idcustomer'] = $row->idcustomer;
        $this->json_data['idpaket'] = $row->idpaket;
        $this->json_data['jenisbayar'] = $row->jenisbayar;
        $this->json_data['total'] = $row->total;
        $this->json_data['date_bayar'] = $row->date_bayar;
        $this->json_data['idpegawai'] = $row->idpegawai;
        echo json_encode($this->json_data);
    }

    function deletetablehistoribayar() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelhistoribayar');
        $this->modelhistoribayar->setDeletehistoribayar($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchhistoribayar() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatahistoribayar'] = $this->getlisthistoribayar($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }

    function simpanhistoribayar() {
        $this->load->helper('json');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $this->load->helper('common');
        $xidcustomer = $_POST['edidcustomer'];
        $xidpaket = $_POST['edidpaket'];
        $xjenisbayar = $_POST['edjenisbayar'];
        $xtotal = $_POST['edtotal'];
        $xdate_bayar = $_POST['eddate_bayar'];
        $xidpegawai = $_POST['edidpegawai'];
        $xidstatusbayar = $_POST['edidstatusbayar'];
        $this->load->model('modelhistoribayar');
        $idpegawai = $this->session->userdata('idpegawai');
        
        if (!empty($idpegawai)) {
//            if ($xidx != '0') {
//                $xStr = $this->modelhistoribayar->setUpdatehistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, date`$xdate_bayar, $xidpegawai);
//            } else {
            $xStr = $this->modelhistoribayar->setInserthistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, str_replace(',', '', $xtotal) , datetomysql($xdate_bayar), $idpegawai);
            //}
        }
        echo json_encode(null);
    }

    ///////////////////////////////////

    function getlistcustomer($xAwal, $xSearch) {
        $xLimit = 20;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('idx', '', 'width=5%') .
                tbaddcell('Name', '', 'width=25%') .
                tbaddcell('Paket', '', 'width=10%') .
                tbaddcell('Payment', '', 'width=10%') .
                tbaddcell('Date', '', 'width=10%') .
                tbaddcell('Status', '', 'width=5%') .
                tbaddcell('Discription', '', 'width=30%') .
                tbaddcell('Save', 'padding:5px;', 'width:5%;text-align:center;'), '', TRUE);
        $this->load->model('modelcustomer');
        $this->load->model('modelpaket');
        $this->load->model('modelhistoribayar');
        $xQuery = $this->modelcustomer->getListcustomer($xAwal, $xLimit, $xSearch);
        foreach ($xQuery->result() as $row) {
            $rowlast = $this->modelhistoribayar->getDetailhistoribayarbyid($row->idx);
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/btnsave.jpg"  alt="Edit Data" onclick = "dosimpanhistoribayar(\'' . $row->idx . '\');" style="border:none;width:40px;height:40px;"/>';
//            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuscustomer(\'' . $row->idx . '\',\'' . substr($row->title, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($row->idx) .
                    tbaddcell($row->title . ' ' . $row->firstname . ' ' . $row->midlename . ' ' . $row->lastname) .
                    tbaddcell(form_dropdown('edidcountry', $this->modelpaket->getArrayListpaket(), @$rowlast->idpaket, ' id="edidpaket_' . $row->idx . '" onChange="dosethargapaket(\'' . $row->idx . '\');" class="require" style="width:200px;" ')) .
                    tbaddcell(form_input(getArrayObj('edtotal_' . $row->idx,  number_format(@$rowlast->total, 2, '.', ',') , '100'), '', 'disabled')) .
                    tbaddcell(form_input(getArrayObj('eddate_bayar_' . $row->idx, datetomysql(@$rowlast->date_bayar), '100'), '', 'class="tanggal"')) .
                    tbaddcell(form_dropdown('edstatusbayar', array('Y'=>'Bayar','N'=>'Belum'), (@$rowlast->statusbayar == 'Y')?'Y':'N', ' id="edidstatusbayar_' . $row->idx . '" class="require"'),(@$rowlast->statusbayar == 'Y')?'background:black;':'background:red;') .
                    tbaddcell(form_textarea(getArrayObj('edjenisbayar_' . $row->idx, @$rowlast->jenisbayar, '200'))) .
                    tbaddcell($xButtonEdit));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchcustomer(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function dosethargapaket() {
        $xidpaket = $_POST['edidpaket'];
        $this->load->helper('json');
        $this->load->model('modelpaket');
        $rowpaket = $this->modelpaket->getDetailpaket($xidpaket);
        $this->json_data['hrgpaket'] = $rowpaket->harga;
        echo json_encode($this->json_data);
    }

    function searchcustomer() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatacustomer'] = $this->getlistcustomer($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }

}

?>
